package cn.devzyh.controller;

import cn.devzyh.domain.ResourceCategory;
import cn.devzyh.domain.ResultVo;
import cn.devzyh.domain.Role;
import cn.devzyh.domain.RoleVo;
import cn.devzyh.service.MenuService;
import cn.devzyh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色模块处理器
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    // 角色列表查询&条件查询
    @PostMapping("/findAllRole")
    public ResultVo findAllRole(@RequestBody Role role) {

        return new ResultVo(true, 200, "响应成功",
                roleService.findByCondition(role));
    }

    // 添加&修改角色
    @PostMapping("/saveOrUpdateRole")
    public ResultVo saveOrUpdateRole(@RequestBody Role role) {

        ResultVo res = new ResultVo(true, 200, "", null);

        if (null == role.getId()) {

            roleService.saveRole(role);
            res.setMessage("角色新增成功");
        } else {

            roleService.updateRole(role);
            res.setMessage("角色修改成功");
        }

        return res;
    }

    // 查询所有菜单列表
    @GetMapping("/findAllMenu")
    public ResultVo findAllMenu() {

        // 查询所有菜单
        Map<String, Object> map = new HashMap<>();
        map.put("parentMenuList", menuService.findByPid(-1));

        return new ResultVo(true, 200, "响应成功", map);
    }

    // 查询角色对应菜单列表
    @GetMapping("/findMenuByRoleId")
    public ResultVo findMenuByRoleId(@RequestParam(value = "roleId", required = true) Integer roleId) {

        // 查询角色对应菜单
        Integer[] menus = roleService.findMenuByRoleId(roleId);

        return new ResultVo(true, 200, "响应成功", menus);
    }

    // 为角色分配菜单
    @PostMapping("/roleContextMenu")
    public ResultVo roleContextMenu(@RequestBody RoleVo roleVo) {

        roleService.roleContextMenu(roleVo.getRoleId(), roleVo.getMenuIdList());

        return new ResultVo(true, 200, "分配成功", null);
    }

    // 删除角色信息
    @GetMapping("/deleteRole")
    public ResultVo deleteRole(@RequestParam(value = "id", required = true) Integer roleId) {

        roleService.deleteRole(roleId);

        return new ResultVo(true, 200, "删除成功", null);
    }

    // 获取当前角色拥有的资源信息
    @GetMapping("/findResourceListByRoleId")
    public ResultVo findResourceListByRoleId(@RequestParam(value = "roleId", required = true) Integer roleId) {

        List<ResourceCategory> list = roleService.findResourceListByRoleId(roleId);

        return new ResultVo(true, 200, "响应成功", list);
    }

    // 为角色分配资源权限
    @PostMapping("/roleContextResource")
    public ResultVo roleContextResource(@RequestBody RoleVo roleVo) {

        roleService.roleContextResource(roleVo);

        return new ResultVo(true, 200, "更新角色资源权限成功", null);
    }
}
